﻿using MediatR;
using Traceability.APP.Applications.Commands.Suyuan;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.APP.Applications.CommandHandlers.Suyuan
{
    public class SuyuanPiDelCommandHandler : IRequestHandler<SuyuanPiDelCommand, APIResult<object>>
    {
        private readonly IBaseRepository<BasicTraceabilityModel> _syRepository;

        public SuyuanPiDelCommandHandler(IBaseRepository<BasicTraceabilityModel> syRepository)
        {
            _syRepository = syRepository;
        }

        public async Task<APIResult<object>> Handle(SuyuanPiDelCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<object>();
            result.Code = APIEnum.DelSuccess;
            var listid = request.Ids.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            foreach (var item in listid)
            {
                var trace = _syRepository.GetAll().FirstOrDefault(x => x.Id == item);
                trace.IsDel = 1;
                await _syRepository.Update(trace);
            }
            result.Message = "批量删除完成";
            return await Task.FromResult(result);
        }
    }
}
